<template>
	<div class="wrapper">
		<!--header部分-->
		<header>
			<div class="back" @click="toWallet">
			<i class="fa fa-chevron-left"></i>
			</div>
			<div class="text">充值</div>
		</header>
		<!--充值金额部分-->
		<ul class="form-box">
			<li>
				<div class="title">
					充值金额：
				</div>
				<div class="content">
					<input type="text" v-model="amount" placeholder="充值金额">
				</div>
			</li>
		</ul>
		<div class="button-cz">
			<button @click="cz">充值</button>
		</div>
	</div>
</template>

<script>
	export default {
		name: 'Cz',
		data() {
			return {
				updateuser:{},
				user: {},
				amount:'',
				usercart:{},
				
			}
		},
		created(){
			this.user = this.$getSessionStorage('user');
			this.$axios.post('UserCartController/getUserCartByUserId',this.$qs.stringify({
				userId:this.user.userId,
			})).then(response=>{
				this.usercart=response.data;
			}).catch(error => {
				console.error(error);
			});
			this.$axios.post('UserController/getUserByUserId',this.$qs.stringify({
				userId:this.user.userId,
			})).then(response=>{
				this.updateuser=response.data;
			}).catch(error => {
				console.error(error);
			});
		},
		
		methods: {
			cz(){
				this.$axios.post('UserCartController/getUserCartByUserId',this.$qs.stringify({
					userId:this.user.userId,
				})).then(response=>{
					let usercart = response.data;
					if(usercart.balance-this.amount<0){
						alert('余额不足！');
					}else{
						this.$axios.post('UserCartController/subBalanceById',this.$qs.stringify({
							userId:this.user.userId,
							amount:this.amount,
							balance:Number(this.usercart.balance)-Number(this.amount),
						})).then(response=>{
							
						}).catch(error=>{
							console.error(error);
						});
						
						this.$axios.post('TranDetailController/saveTranDetail',this.$qs.stringify({
							userId:this.user.userId,
							amount:this.amount,
							trantype:'1',
						})).then(response=>{
							
						}).catch(error=>{
							console.error(error);
						});
						
						this.$axios.post('UserController/addBalanceById',this.$qs.stringify({
							userId:this.user.userId,
							amount:this.amount,
							walletBalance:Number(this.updateuser.walletBalance)+Number(this.amount),
						})).then(response=>{
							alert('充值成功！');
						}).catch(error=>{
							console.error(error);
						});
						
					}
				}).catch(error=>{
					console.error(error);
				});
			},
			
			toWallet() {
				this.$router.push({
					path: '/wallet'
				});
			},
			
			
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
		background-color: azure;
	}
	/****************** header ******************/
	.wrapper header{
		width: 100%;
		height: 12vw;
		background-color: #FFF;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin-bottom: 1vw;
	}
	.wrapper header .back{
		font-size: 4vw;
		margin-left: 2vw;
		margin-right: 2vw;
	}
	.wrapper header .text{
		font-size: 4vw;
	}
	
	.wrapper .form-box {
		width: 100%;
		
	}
	
	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}
	
	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}
	
	.wrapper .form-box li .content {
		flex: 1;
	}
	
	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}
	
	.wrapper .button-cz {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}
	
	.wrapper .button-cz button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;
		border: none;
		outline: none;
	}
</style>
